import uuid
from django.db import models

"""
Django 数据库迁移命令: 
python manage.py makemigrations
python manage.py migrate
.venv\Scripts\activate
"""

# 用户表
class User(models.Model):
    user_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    username = models.CharField(max_length=6,  verbose_name='用户名', unique=True)
    password = models.CharField(max_length=32, verbose_name='密码')
    # 角色
    role = models.IntegerField(default=2, verbose_name='角色', choices=(
        (1, "管理员"),
        (2, "运维"),
    ))
    # 状态
    status = models.IntegerField(default=1, verbose_name='状态', choices=(
        (1, "正常"),
        (2, "禁用"),
    ))
    # 头像
    avatar = models.FileField(upload_to='media/avatar/',blank=True, null=True, verbose_name='头像', default='Logo/167x454.png')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'user'
        verbose_name = '用户'
        verbose_name_plural = '用户'

# 平台
class Platform(models.Model):
    STATUS_CHOICES = (
        (1, "运行中"),
        (2, "禁用"),
    )
    platform_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, verbose_name='平台ID')
    name = models.CharField(max_length=50, unique=True, verbose_name='平台名称')  # 如"微信"、"QQ"
    # 官网
    website = models.URLField(max_length=255, blank=True, null=True, verbose_name='官网')
    description = models.TextField(blank=True, null=True, verbose_name='平台描述')
    status = models.IntegerField(choices=STATUS_CHOICES, default=1, verbose_name='状态')
    # logo
    logo = models.URLField(max_length=255, blank=True, null=True, verbose_name='logo')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'platform'
        verbose_name = '平台'
        verbose_name_plural = '平台'
        ordering = ['-create_time']



# 账号
class Account(models.Model):
    account_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, verbose_name='账号ID')
    username = models.CharField(max_length=50, unique=True, verbose_name='用户名')
    password = models.CharField(max_length=128, verbose_name='密码')
    phone = models.CharField(max_length=20, blank=True, null=True, unique=True, verbose_name='手机号')
    # 充值金额
    balance = models.DecimalField(max_digits=10, decimal_places=2, default=0, null=True, blank=True, verbose_name='余额')
    # 创建人
    create_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='created_accounts', verbose_name='创建人')
    # 当前所属人
    current_owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='owned_accounts', verbose_name='当前所属人', null=True, blank=True)
    # 所属平台
    platform = models.ManyToManyField(Platform,through='Platform_Account', related_name='accounts', verbose_name='所属平台')
    
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')


    class Meta:
        db_table = 'account'
        verbose_name = '账号'
        verbose_name_plural = '账号'
        ordering = ['-create_time']

class Platform_Account(models.Model):
    STATUS_CHOICES = (
        (1, "正常"),
        (2, "封号"),
    )
    platform = models.ForeignKey(Platform, on_delete=models.CASCADE, related_name='platform_accounts', verbose_name='平台')
    account = models.ForeignKey(Account, on_delete=models.CASCADE, related_name='platform_accounts', verbose_name='账号')

    # 状态
    status = models.IntegerField(choices=STATUS_CHOICES, default=1, verbose_name='状态')
    # 是否实名
    is_real_name = models.BooleanField(default=False, verbose_name='是否实名')
    # 实名姓名
    real_name = models.CharField(max_length=10, blank=True, null=True, verbose_name='实名姓名')
    # 身份证号
    id_card = models.CharField(max_length=18, blank=True, null=True, verbose_name='身份证号')


# 订单表
class OrderList(models.Model):
    # 订单状态选项 - 数字表示
    STATUS_CHOICES = (
        (1, "待接手"),
        (2, "代练中"),
        (3, "已完成"),
        (4, "已取消"),
        (5, "处理中"),
        (6, "已失败")
    )
    account = models.ForeignKey(Account, on_delete=models.CASCADE, related_name='orders', verbose_name='账号')
    order_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    # 订单来源名称
    order_source_name = models.CharField(max_length=255, blank=True, null=True, verbose_name='订单来源名称')
    # 发布主平台名称
    posting_platform_name = models.CharField(max_length=255, blank=True, null=True, verbose_name='发布主平台名称')
    # 订单来源ID
    order_source_id = models.CharField(max_length=255, blank=True, null=True, verbose_name='订单来源ID')
    # 订单发布平台ID
    posting_platform_id = models.CharField(max_length=255, blank=True, null=True, verbose_name='订单发布平台ID')

    # 2. 订单名称
    order_name = models.CharField(max_length=255, blank=True, null=True)
    # 3. 接单价格
    receiving_price = models.DecimalField(max_digits=10, decimal_places=2, default=0, null=True, blank=True)
    # 4. 接单保证金
    receiving_margin = models.DecimalField(max_digits=10, decimal_places=2, default=0, null=True, blank=True)
    # 5. 接单效率金
    receiving_efficiency_fee = models.DecimalField(max_digits=10, decimal_places=2, default=0, null=True, blank=True)
    # 6. 接单代练时间
    receiving_delivery_time = models.CharField(max_length=20, blank=True, null=True, verbose_name='接单代练时间')
    # 7. 接单区服
    receiving_server = models.CharField(max_length=20, blank=True, null=True, verbose_name='接单区服')
    # 8. 发单价格
    posting_price = models.DecimalField(max_digits=10, decimal_places=2, default=0, null=True, blank=True)
    # 9. 发单安全金
    posting_security_fee = models.DecimalField(max_digits=10, decimal_places=2, default=0, null=True, blank=True)
    # 10. 发单效率金
    posting_efficiency_fee = models.DecimalField(max_digits=10, decimal_places=2, default=0, null=True, blank=True)
    # 13. 订单状态
    status = models.IntegerField(choices=STATUS_CHOICES, default=1)
    # 14. 创建时间
    create_time = models.DateTimeField(auto_now_add=True)
    # 15. 更新时间
    update_time = models.DateTimeField(auto_now=True)
    # 号主联系方式
    owner_contact = models.TextField(blank=True, null=True, verbose_name='号主联系方式')
    # 代练联系方式
    dailian_contact = models.TextField(blank=True, null=True, verbose_name='代练联系方式')
    # 20. 备注
    remark = models.TextField(blank=True, null=True)
    # 本单总收入
    completed_profit = models.DecimalField(max_digits=10, decimal_places=2, default=0, null=True, blank=True, verbose_name="订单总收入")


    
    class Meta:
        db_table = 'order_list'
        verbose_name = '订单'
        verbose_name_plural = '订单'
        ordering = ['-create_time']


# 版本
class Version(models.Model):
    STATUS_CHOICES = (
        (1, "正常"),
        (2, "维护"),
    )
    version_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, verbose_name='版本ID')
    version = models.CharField(max_length=20, verbose_name='版本号', unique=True)  # 语义化版本号字符串
    major = models.IntegerField(verbose_name='主版本号')  # 主版本号
    minor = models.IntegerField(verbose_name='次版本号')  # 次版本号
    patch = models.IntegerField(verbose_name='修订号')  # 修订号
    # 版本描述
    version_desc = models.TextField(blank=True, null=True, verbose_name='版本描述')
    # 版本状态
    status = models.IntegerField(choices=STATUS_CHOICES, default=1, verbose_name='版本状态')
    # 版本下载地址
    download_url = models.CharField(max_length=255, verbose_name='版本下载地址')
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'version'
        verbose_name = '版本'
        verbose_name_plural = '版本'
        ordering = ['-major', '-minor', '-patch']  # 按主、次、修订号降序排序


# 话术分类
class ScriptCategory(models.Model):
    STATUS_CHOICES = (
        (1, "启用"),
        (2, "禁用"),
    )
    category_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, verbose_name='分类ID')
    name = models.CharField(max_length=50, unique=True, verbose_name='分类名称')  # 如"代练中"、"撤单中"
    description = models.TextField(blank=True, null=True, verbose_name='分类描述')
    status = models.IntegerField(choices=STATUS_CHOICES, default=1, verbose_name='状态')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'script_category'
        verbose_name = '话术分类'
        verbose_name_plural = '话术分类'
        ordering = ['-create_time']


# 话题话术
class TopicScript(models.Model):
    STATUS_CHOICES = (
        (1, "启用"),
        (2, "禁用"),
    )
    script_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, verbose_name='话术ID')
    category = models.ForeignKey(ScriptCategory, on_delete=models.CASCADE, related_name='scripts', verbose_name='所属分类')
    content = models.TextField(verbose_name='话术内容')
    description = models.TextField(blank=True, null=True, verbose_name='话术描述')
    order = models.IntegerField(default=0, verbose_name='排序')  # 排序字段
    status = models.IntegerField(choices=STATUS_CHOICES, default=1, verbose_name='状态')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'topic_script'
        verbose_name = '话题话术'
        verbose_name_plural = '话题话术'
        ordering = ['category', 'order', '-create_time']


# 反馈
class Feedback(models.Model):
    STATUS_CHOICES = (
        (1, "待处理"),
        (2, "已处理"),
    )
    FEEDBACK_TYPE_CHOICES = (
        (1, "问题反馈"),
        (2, "建议反馈"),
        (3, "其他反馈"),
    )
    feedback_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, verbose_name='反馈ID')
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, related_name='feedbacks', verbose_name='用户')
    feedback_type = models.IntegerField(choices=FEEDBACK_TYPE_CHOICES, default=1, verbose_name='反馈类型')
    content = models.TextField(verbose_name='反馈内容')
    handler = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, related_name='handled_feedbacks', verbose_name='处理人')
    status = models.IntegerField(choices=STATUS_CHOICES, default=1, verbose_name='状态')
    # 处理结果
    result = models.TextField(blank=True, null=True, verbose_name='处理结果')
    
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    

    class Meta:
        db_table = 'feedback'
        verbose_name = '反馈'
        verbose_name_plural = '反馈'
        ordering = ['-create_time']

# 反馈的照片
class FeedbackPhoto(models.Model):
    # 反馈的照片类型,反馈人、处理人
    PHOTOTYPE_CHOICES = (
        (1, "反馈人"),
        (2, "处理人"),
    )
    photo_type = models.IntegerField(choices=PHOTOTYPE_CHOICES, default=1, verbose_name='照片类型')
    feedback = models.ForeignKey(Feedback, on_delete=models.CASCADE, related_name='photos', verbose_name='反馈')
    photo = models.FileField(upload_to='feedback_photos/', verbose_name='照片')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'feedback_photo'
        verbose_name = '反馈照片'
        verbose_name_plural = '反馈照片'
        ordering = ['-create_time']
